﻿using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("Transform/Mirror/Left To Right",
        "Mirrors child transforms based of their names. Supported conventions; contains left/right, prefix l/r, suffix l/r.",
        ContextRequirements.SingleItem, 0)]
        public static void TransformMirrorXSL()
        {
            var contextItem = SkinnContext.GetContextItem();
            var transform = contextItem.transform;
            var mirrored = transform.MirrorHumanoidGeneric(true, null);
            if (mirrored) Undo.RegisterCompleteObjectUndo(mirrored, "Mirror Left To Right");
            SkinnContext.Finished();
        }

        [CtxMenuItem("Transform/Mirror/Right To Left",
         "Mirrors child transforms based of their names. Supported conventions; contains left/right, prefix l/r, suffix l/r.",
         ContextRequirements.SingleItem, 0)]
        public static void TransformMirrorXSR()
        {
            var contextItem = SkinnContext.GetContextItem();
            var transform = contextItem.transform;
            var mirrored = transform.MirrorHumanoidGeneric(false, null);
            if (mirrored) Undo.RegisterCompleteObjectUndo(mirrored, "Mirror Right To Left");
            SkinnContext.Finished();
        }

        [CtxMenuItem("Transform/Mirror/Children/Left To Right",
          "Mirrors child transforms based of their names. Supported conventions; contains left/right, prefix l/r, suffix l/r.",
          ContextRequirements.Children, 0)]
        public static void TransformMirrorXCL()
        {
            var contextItem = SkinnContext.GetContextItem();
            var transforms = contextItem.transform.GetAllChildren(true);
            foreach (var transform in transforms)
            {
                var mirrored = transform.MirrorHumanoidGeneric(true, transforms);
                if (mirrored) Undo.RegisterCompleteObjectUndo(mirrored, "Mirror Left To Right");
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Transform/Mirror/Children/Right To Left",
         "Mirrors child transforms based of their names. Supported conventions; contains left/right, prefix l/r, suffix l/r.",
         ContextRequirements.Children, 0)]
        public static void TransformMirrorXCR()
        {
            var contextItem = SkinnContext.GetContextItem();
            var transforms = contextItem.transform.GetAllChildren(true);
            foreach (var transform in transforms)
            {
                var mirrored = transform.MirrorHumanoidGeneric(false, transforms);
                if (mirrored) Undo.RegisterCompleteObjectUndo(mirrored, "Mirror Right To Left");
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Transform/Mirror/All/Left To Right",
                "Mirrors child transforms based of their names. Supported conventions; contains left/right, prefix l/r, suffix l/r.",
                ContextRequirements.Children, 0)]
        public static void TransformMirrorXL()
        {
            var contextItem = SkinnContext.GetContextItem();
            var transforms = contextItem.transform.root.GetAllChildren(true);
            foreach (var transform in transforms)
            {
                var mirrored = transform.MirrorHumanoidGeneric(true, transforms);
                if (mirrored) Undo.RegisterCompleteObjectUndo(mirrored, "Mirror Left To Right");
            }
            SkinnContext.Finished();
        }

        [CtxMenuItem("Transform/Mirror/All/Right To Left",
          "Mirrors child transforms based of their names. Supported conventions; contains left/right, prefix l/r, suffix l/r.",
          ContextRequirements.Children, 0)]
        public static void TransformMirrorXR()
        {
            var contextItem = SkinnContext.GetContextItem();
            var transforms = contextItem.transform.root.GetAllChildren(true);
            foreach (var transform in transforms)
            {
                var mirrored = transform.MirrorHumanoidGeneric(false, transforms);
                if (mirrored) Undo.RegisterCompleteObjectUndo(mirrored, "Mirror Right To Left");
            }
            SkinnContext.Finished();
        }
    }
}
